// components/loginPopup/index.js
import Message from 'tdesign-miniprogram/message/index';
const app = getApp()
const {
  login
} = require('../../../../api/index')
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    IMGBASEURL: app.globalData.IMGBASEURL,
    visible: false,
    isCanFast: false,
    isNavBar: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // onConsentClose() {
    //   this.setData({
    //     visible: false,
    //     isNavBar: true
    //   })
    // },
    getPhoneNumber(e) {
     
      if(!e.detail.code) return
      if (this.data.isCanFast) {
      login.phoneAndWxLogin({
        client_id: 'House_App',
        scope: 'openid profile house',
        grant_type: 'sms',
        minicode: e.detail.code
      }).then((res) => {
        if (res) {
          wx.setStorageSync('token', `${res.token_type} ${res.access_token}`)
          app.getUser().then((res) => {
            // this.selectComponent('#consent').handlePopup()
            if (res.IsNewUser) {
              app.globalData.sdk.register(res.UserId)
            }
            app.globalData.sdk.loggedin(res.UserId)
            if (res) {
              if (this.data.isNavBar) {
                this.getTabBar()?.setListData(true)
              }
              // this.selectComponent('#consent').handlePopup()
              app.getUserStatus(this).then()
              this.setData({
                visible: false,
                isNavBar: true
              })
              if (res.UserInfoStatus != 8) {
                app.loadSignalR()
              }
            }
          })
        }
      })
    }
      // login.getphone({code:e.detail.code}).then(res=>{
      //   this.setData({
      //     phoneNumber:res.phoneNumber
      //   })
      // })
    },
    fastLogin() {
      if (this.data.isCanFast) {
        wx.showLoading()
        app.wxfastLogin().then(e => {
          if (e) {
            if (this.data.isNavBar) {
              this.getTabBar()?.setListData(true)
            }
            // this.selectComponent('#consent').handlePopup()
            this.setData({
              visible: false,
              isNavBar: true
            })
          }
        })
      } else {
        Message.warning({
          context: this,
          offset: [20, 32],
          duration: 1000,
          content: '请先阅读并勾选用户协议和隐私协议',
        });
      }
    },
    onCLI(){
      Message.warning({
        context: this,
        offset: [20, 32],
        duration: 1000,
        content: '请先阅读并勾选用户协议和隐私协议',
      });
    },
    selectConent(event) {
      this.setData({
        isCanFast : event.detail.isConsent
      })
    },
    onVisibleChange(e) {
      this.setData({
        visible: e.detail.visible,
      });
    },
    goPage(e) {
      wx.navigateTo({
        url: e.currentTarget.dataset['index'],
      })
    }
  }
})